package com.pbl6.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pbl6.back.domain.Liaisons;
import com.pbl6.back.domain.User;
import com.pbl6.back.service.ILiaisonsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xin.altitude.cms.common.entity.AjaxResult;
import xin.altitude.cms.common.entity.PageEntity;

import java.util.Arrays;
@RestController
@RequestMapping("/back/liaisons")
public class LiaisonsController{
    @Autowired
    private ILiaisonsService liaisonsService;
    @GetMapping("/page")
    public AjaxResult page(PageEntity pageEntity,Liaisons liaisons){
        return AjaxResult.success(liaisonsService.page(pageEntity.toPage(), Wrappers.lambdaQuery(liaisons)));
    }
    @GetMapping("/list")
    public AjaxResult list(Liaisons liaisons){
        LambdaQueryWrapper<Liaisons> recordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (liaisons.getOperator() !=null&& !" ".equals(liaisons.getOperator())) {
            recordLambdaQueryWrapper.like(Liaisons::getOperator,liaisons.getOperator());
        }
        if (liaisons.getName() !=null&& !" ".equals(liaisons.getName())) {
            recordLambdaQueryWrapper.like(Liaisons::getName,liaisons.getName());
        }
        if (liaisons.getSex() !=null&& !" ".equals(liaisons.getSex())) {
            recordLambdaQueryWrapper.like(Liaisons::getSex,liaisons.getSex());
        }
        if (liaisons.getEmployer() !=null&& !" ".equals(liaisons.getEmployer())) {
            recordLambdaQueryWrapper.like(Liaisons::getEmployer,liaisons.getEmployer());
        }
        return AjaxResult.success(liaisonsService.list(Wrappers.lambdaQuery(liaisons)));
    }
    @PostMapping("/push")
    public AjaxResult add(@RequestBody Liaisons liaisons) {
        return AjaxResult.success(liaisonsService.save(liaisons));
    }
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Liaisons liaisons) {
        return AjaxResult.success(liaisonsService.updateById(liaisons));
    }
    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        return AjaxResult.success(liaisonsService.removeByIds(Arrays.asList(ids)));
    }
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Integer id) {
        return AjaxResult.success(liaisonsService.getById(id));
    }
}
